<?php

/**
 * This is the model class for table "vehiculos".
 *
 * The followings are the available columns in table 'vehiculos':
 * @property integer $id
 * @property integer $idCombustible
 * @property integer $idTipoVehiculo
 * @property integer $idProveedor
 * @property integer $idMarca
 * @property integer $idModelo
 * @property integer $idColor
 * @property string $ano
 * @property string $patente
 * @property integer $precioCompra
 * @property integer $estado
 * @property string $foto
 * @property string $creado
 * @property string $modificado
 *
 * The followings are the available model relations:
 * @property HistorialVehiculos[] $historialVehiculoses
 * @property OrdenTrabajo[] $ordenTrabajos
 * @property Combustibles $idCombustible0
 * @property TiposVehiculos $idTipoVehiculo0
 * @property Proveedores $idProveedor0
 * @property MarcasVehiculos $idMarca0
 * @property ModelosVehiculos $idModelo0
 * @property ColoresVehiculos $idColor0
 */
class Vehiculos extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Vehiculos the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'vehiculos';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idCombustible, idTipoVehiculo, idProveedor, idMarca, idModelo, ano, patente, precioCompra, estado, creado, modificado', 'required'),
			array('idCombustible, idTipoVehiculo, idProveedor, idMarca, idModelo, idColor, precioCompra, estado', 'numerical', 'integerOnly'=>true),
			array('ano', 'length', 'max'=>4),
			array('patente', 'length', 'max'=>6),
			array('foto', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, idCombustible, idTipoVehiculo, idProveedor, idMarca, idModelo, idColor, ano, patente, precioCompra, estado, foto, creado, modificado', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'historialVehiculoses' => array(self::HAS_MANY, 'HistorialVehiculos', 'id_vehiculo'),
			'ordenTrabajos' => array(self::HAS_MANY, 'OrdenTrabajo', 'id_vehiculo'),
			'idCombustible0' => array(self::BELONGS_TO, 'Combustibles', 'idCombustible'),
			'idTipoVehiculo0' => array(self::BELONGS_TO, 'TiposVehiculos', 'idTipoVehiculo'),
			'idProveedor0' => array(self::BELONGS_TO, 'Proveedores', 'idProveedor'),
			'idMarca0' => array(self::BELONGS_TO, 'MarcasVehiculos', 'idMarca'),
			'idModelo0' => array(self::BELONGS_TO, 'ModelosVehiculos', 'idModelo'),
			'idColor0' => array(self::BELONGS_TO, 'ColoresVehiculos', 'idColor'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'idCombustible' => 'Id Combustible',
			'idTipoVehiculo' => 'Id Tipo Vehiculo',
			'idProveedor' => 'Id Proveedor',
			'idMarca' => 'Id Marca',
			'idModelo' => 'Id Modelo',
			'idColor' => 'Id Color',
			'ano' => 'Ano',
			'patente' => 'Patente',
			'precioCompra' => 'Precio Compra',
			'estado' => 'Estado',
			'foto' => 'Foto',
			'creado' => 'Creado',
			'modificado' => 'Modificado',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('idCombustible',$this->idCombustible);
		$criteria->compare('idTipoVehiculo',$this->idTipoVehiculo);
		$criteria->compare('idProveedor',$this->idProveedor);
		$criteria->compare('idMarca',$this->idMarca);
		$criteria->compare('idModelo',$this->idModelo);
		$criteria->compare('idColor',$this->idColor);
		$criteria->compare('ano',$this->ano,true);
		$criteria->compare('patente',$this->patente,true);
		$criteria->compare('precioCompra',$this->precioCompra);
		$criteria->compare('estado',$this->estado);
		$criteria->compare('foto',$this->foto,true);
		$criteria->compare('creado',$this->creado,true);
		$criteria->compare('modificado',$this->modificado,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function beforeValidate() {
            if ($this->isNewRecord)
                $this->creado = new CDbExpression('NOW()');

            $this->modificado = new CDbExpression('NOW()');

            return parent::beforeSave();
        }
}